<?php

  //////////////////////////////////////////////////////////////////
  // OrbitFAQ                                                     //
  // ---------                                                    //
  //                                                              //
  // Orbit FAQ was solely written and developed by Orbit Services //
  // http://www.orbitservices.net                                 //
  //                                                              //
  // Access the Forum here:                                       //
  // http://forums.orbitservices.net/index.php?c=4                //
  //                                                              //
  // OrbitFAQ utilises the following opensource projects/classes; //
  //  + Fckeditor - http://www.fckeditor.net                      //
  //  + Smarty Template Engine - http://smarty.php.net            //
  //  + Swift Email Class - http://www.swiftmailer.org/           //
  //  + OWASP PHP Filter Project - http://www.owasp.org           //
  //  + MySQL Search Class by Stephen Bartholomew                 //
  //                                                              //
  //////////////////////////////////////////////////////////////////

  $smarty->assign("OrbitFAQTitle","Question Management");

  // Standard Functions
  function ReOrderQNS($ca)
    {

      require('../incs/config.inc.php');
      require('../incs/db.inc.php');

      $new_order = '0';

      $query_cat_order = "SELECT * from orbitfaq_categories ORDER BY cat_order ASC";
      $result_cat_order = $faqsql_query ($query_cat_order)OR DIE( "$sql_query_error $query_cat_order");

      while ($row_cat_order = $faqsql_fetch_array ($result_cat_order)){
        $cat_id_order = $row_cat_order[0];

        $new_order = '0';

          $query_qn_order = "SELECT * from orbitfaq_questions WHERE `c_id` = '$cat_id_order' ORDER BY qn_order ASC";
          $result_qn_order = $faqsql_query ($query_qn_order)OR DIE( "$sql_query_error $query_qn_order");

          while ($row_qn_order = $faqsql_fetch_array ($result_qn_order)){
            $qn_id_order = $row_qn_order[0];

            $new_order = $new_order+10;

            $query_update_order = "UPDATE `orbitfaq_questions` SET `qn_order` = '$new_order' WHERE `id` = '$qn_id_order';";
            $result_update_order = $faqsql_query ($query_update_order)OR DIE( "$sql_query_error $query_update_order");

          }

      }

    }

  if($action != '')
    {
      $smarty_template_file = "$AdminFullPath/skins/$AdminSkin/mod_". $mod ."_". $action .".tpl";

      if($action == 'create')
        {

          if($posted == 'yes')
            {

              $posted_desc = $_POST['posted_desc'];
              $posted_ans = $_POST['posted_ans'];
              $posted_cat = sanitize_paranoid_string($_POST['posted_cat']);
              $posted_associated_answer = sanitize_paranoid_string($_POST['posted_associated_answer']);

              if($posted_associated_answer == 'undefined')
                {
                  $posted_associated_answer = '';
                }

              if(!$posted_cat){ $error .= " &raquo; You did not select a <u>Category</u><br />"; }
              if(!$posted_desc){ $error .= " &raquo; You did not enter a <u>Question</u><br />"; }
              if((!$posted_ans)AND(!$posted_associated_answer)){ $error .= " &raquo; You did not enter or select an associated <u>Answer</u><br />"; }

              $query_faq_pd = "SELECT f_id from `orbitfaq_categories` WHERE `id` = '$posted_cat'";
              $result_faq_pd = $faqsql_query ($query_faq_pd)OR DIE( "$sql_query_error $query_faq_pd");

              while ($row_faq_pd = $faqsql_fetch_array ($result_faq_pd)){
                $faq_pd_id = $row_faq_pd[0];
              }

              if(!$error)
                {

                  $posted_desc = htmlentities($_POST['posted_desc'], ENT_QUOTES);
                  $posted_ans = htmlentities($_POST['posted_ans'], ENT_QUOTES);

                  $posted_time = date("Y-m-d H:i:s");

                  $query_add = "INSERT INTO `orbitfaq_questions` (
                      `f_id`,
                      `c_id`,
                      `a_id`,
                      `desc`,
                      `posted_time`,
                      `posted_by`,
                      `posted_email`,
                      `status`,
                      `ip`
                    )VALUES(
                      '$faq_pd_id',
                      '$posted_cat',
                      '$posted_associated_answer',
                      '$posted_desc',
                      '$posted_time',
                      'FAQ Admin',
                      'norepy@domain.com',
                      '1',
                      '0.0.0.0'
                    );

                  ";
                  $result_add = $faqsql_query ($query_add)OR DIE( "$sql_query_error $query_add");
                  $q_id = $faqsql_insertid();

                  if(($posted_associated_answer == '')OR($posted_associated_answer == '0'))
                    {

                      $query_add_ans = "INSERT INTO `orbitfaq_answers` (
                          `desc`,
                          `posted_time`,
                          `posted_by`
                        )VALUES(
                          '$posted_ans',
                          '$posted_time',
                          'FAQ Admin'
                        );

                      ";

                      $result_add_ans = $faqsql_query ($query_add_ans)OR DIE( "$sql_query_error $query_add_ans");
                      $a_id = $faqsql_insertid();

                      // Set our Answer ID
                      $query_update = "UPDATE `orbitfaq_questions` SET `a_id` = '$a_id' WHERE `id` = '$q_id';";
                      $result_update = $faqsql_query ($query_update)OR DIE( "$sql_query_error $query_update");

                    }

                  $order = ReOrderQNS($posted_cat);

                  clearSmartyCompiled($SmartyCompileDir, $ClearSmartyCompiled);

                  $message = "New Question Was Successfully Created!";
                  header("Location: index.php?mod=$mod&message=$message");

                  exit;

                }
              else
                {

                  $posted_desc = preg_replace( "/\n/", "", $posted_desc);
                  $posted_desc = preg_replace( "/\r/", "", $posted_desc);

                  $posted_ans = preg_replace( "/\n/", "", $posted_ans);
                  $posted_ans = preg_replace( "/\r/", "", $posted_ans);

                  $smarty->assign("posted_associated_answer","$posted_associated_answer");
                  $smarty->assign("posted_ans","$posted_ans");
                  $smarty->assign("posted_desc","$posted_desc");
                  $smarty->assign("posted_cat","$posted_cat");
                  $smarty->assign("Errors","$error");

                }

            }

            // Lets get our Categories
            $query_cat_pd = "SELECT * from orbitfaq_categories WHERE `f_id` = ANY(SELECT f_id from orbitfaq_admins_ownership WHERE `admin_id` = '$orbitfaq_userid') ORDER BY f_id, cat_order ASC";
            $result_cat_pd = $faqsql_query ($query_cat_pd)OR DIE( "$sql_query_error $query_cat_pd");
            $count_cat_pd = $faqsql_count_rows($result_cat_pd);

            while ($row_cat_pd = $faqsql_fetch_array ($result_cat_pd)){
              $cat_pd_id = $row_cat_pd[0];
              $cat_pd_f_id = $row_cat_pd[1];
              $cat_pd_title = $row_cat_pd[2];

              $query_faq_pd = "SELECT title from `orbitfaq` WHERE `id` = '$cat_pd_f_id'";
              $result_faq_pd = $faqsql_query ($query_faq_pd)OR DIE( "$sql_query_error $query_faq_pd");
              $count_faq_pd = $faqsql_count_rows($result_faq_pd);

              while ($row_faq_pd = $faqsql_fetch_array ($result_faq_pd)){
                $faq_pd_title = $row_faq_pd[0];
              }

              if((!$posted_cat)AND($ca == $cat_pd_id))
                {
                  $selected = " selected";
                }
              elseif(($posted_cat)AND($cat_pd_id == $posted_cat))
                {
                  $selected = " selected";
                }
              else
                {
                  $selected = "";
                }

              $build_cat_pulldown .= "<option value='$cat_pd_id' $selected>$faq_pd_title > $cat_pd_title</option>\n";

            }

            $smarty->assign("CATPulldown","$build_cat_pulldown");

      } // End Create
    elseif($action == 'modify')
      {
        if($qn != '')
          {

            // Lets check that this user has access to this faq
            $SuperAdmin = CheckAdminAccess($faq, $ca, $qn, $accesslvl, $orbitfaq_userid);

            if($SuperAdmin == '0')
              {

                clearSmartyCompiled($SmartyCompileDir, $ClearSmartyCompiled);

                $message = "You do not have access to modify this Question because it is assigned to a FAQ you do not have access to!";
                header("Location: index.php?mod=$mod&message=$message");

                exit;
              }

          if($posted == 'yes')
            {

              $posted_ans = $_POST['posted_ans'];
              $posted_desc = $_POST['posted_desc'];
              $posted_cat = sanitize_paranoid_string($_POST['posted_cat']);
              $posted_addasnew = sanitize_paranoid_string($_POST['posted_addasnew']);
              $posted_associated_answer = sanitize_paranoid_string($_POST['posted_associated_answer']);

              if($posted_associated_answer == 'undefined')
                {
                  $posted_associated_answer = '';
                }


              if(!$posted_cat){ $error .= " &raquo; You did not select a <u>Category</u><br />"; }
              if((!$posted_ans)AND(!$posted_associated_answer)){ $error .= " &raquo; You did not enter or select an associated <u>Answer</u><br />"; }
              if(!$posted_desc){ $error .= " &raquo; You did not enter a <u>Question</u><br />"; }


              if(!$error)
                {

                  $query_faq_pd = "SELECT f_id from `orbitfaq_categories` WHERE `id` = '$posted_cat'";
                  $result_faq_pd = $faqsql_query ($query_faq_pd)OR DIE( "$sql_query_error $query_faq_pd");

                  while ($row_faq_pd = $faqsql_fetch_array ($result_faq_pd)){
                    $faq_pd_id = $row_faq_pd[0];
                  }

                  $query_update = "UPDATE `orbitfaq_questions` SET
                      `f_id` = '$faq_pd_id',
                      `c_id` = '$posted_cat',
                      `desc` = '$posted_desc',
                      `status` = '1'
                      WHERE `id` = '$qn'
                    ;

                  ";

                  $result_update = $faqsql_query ($query_update)OR DIE( "$sql_query_error $query_update");

                  if(($posted_associated_answer == '')OR($posted_associated_answer == '0'))
                    {

                      $query_qn = "SELECT a_id, notice_sent from orbitfaq_questions WHERE `id` = '$qn'";
                      $result_qn = $faqsql_query ($query_qn)OR DIE( "$sql_query_error $query_qn");

                      while ($row_qn = $faqsql_fetch_array ($result_qn)){
                        $qn_a_id = $row_qn[0];
                      }

                      if(($posted_addasnew == '1')OR($qn_a_id == '0'))
                        {

                          $posted_desc = htmlentities($_POST['posted_desc'], ENT_QUOTES);
                          $posted_ans = htmlentities($_POST['posted_ans'], ENT_QUOTES);
                          $posted_time = date("Y-m-d H:i:s");

                          $query_add_ans = "INSERT INTO `orbitfaq_answers` (
                              `desc`,
                              `posted_time`,
                              `posted_by`
                            )VALUES(
                              '$posted_ans',
                              '$posted_time',
                              'FAQ Admin'
                            );

                          ";

                          $result_add_ans = $faqsql_query ($query_add_ans)OR DIE( "$sql_query_error $query_add_ans");
                          $a_id = $faqsql_insertid();

                          // Set our Answer ID
                          $query_update = "UPDATE `orbitfaq_questions` SET `a_id` = '$a_id' WHERE `id` = '$qn';";
                          $result_update = $faqsql_query ($query_update)OR DIE( "$sql_query_error $query_update");

                        }
                      else
                        {

                          $posted_desc = htmlentities($_POST['posted_desc'], ENT_QUOTES);
                          $posted_ans = htmlentities($_POST['posted_ans'], ENT_QUOTES);

                          $query_update = "UPDATE `orbitfaq_answers` SET
                              `desc` = '$posted_ans'
                              WHERE `id` = '$qn_a_id'
                            ;

                          ";

                          $result_update = $faqsql_query ($query_update)OR DIE( "$sql_query_error $query_update");

                        }


                    }
                  else
                    {

                      // Set our Answer ID
                      $query_update = "UPDATE `orbitfaq_questions` SET `a_id` = '$posted_associated_answer' WHERE `id` = '$qn';";
                      $result_update = $faqsql_query ($query_update)OR DIE( "$sql_query_error $query_update");

                    }

                $query_qn_notice = "SELECT * from orbitfaq_questions WHERE `id` = '$qn'";
                $result_qn_notice = $faqsql_query ($query_qn_notice)OR DIE( "$sql_query_error $query_qn_notice");
                $count_qn_notice = $faqsql_count_rows($result_qn_notice);

                while ($row_qn_notice = $faqsql_fetch_array ($result_qn_notice)){
                  $qn_notice_id = $row_qn_notice[0];
                  $qn_notice_f_id = $row_qn_notice[1];
                  $qn_notice_c_id = $row_qn_notice[2];
                  $qn_notice_a_id = $row_qn_notice[3];
                  $qn_notice_desc = $row_qn_notice[4];
                  $qn_notice_posted_email = $row_qn_notice[7];
                  $qn_notice_posted_email = trim($qn_notice_posted_email);

                  $query_ans_notice = "SELECT * from orbitfaq_answers WHERE `id` = '$qn_notice_a_id'";
                  $result_ans_notice = $faqsql_query ($query_ans_notice)OR DIE( "$sql_query_error $query_ans_notice");
                  $count_ans_notice = $faqsql_count_rows($result_ans_notice);

                  if($count_ans_notice == '0')
                    {
                      $ans_desc_notice = '';
                    }

                  while ($row_ans_notice = $faqsql_fetch_array ($result_ans_notice)){
                    $ans_notice_id = $row_ans_notice[0];
                    $ans_notice_desc = $row_ans_notice[1];
                    $ans_notice_posted_by = $row_ans_notice[2];
                  }

                  $qn_notice_desc = html_entity_decode($qn_notice_desc);
                  $ans_notice_desc = html_entity_decode($ans_notice_desc);

                    // Get our email address and email them the notice

                    $OurMessage = "<font face='verdana'>Please note that your question raised at <b>$OrbitFAQTitle</b> has been answered!<br /><br />";
                    $OurMessage .= "Go to the following link to view the answer to your question<br />";
                    $OurMessage .= "$FaqAddress/index.php?faq=$qn_notice_f_id&ca=$qn_notice_c_id#qn$qn_notice_id<br /><br />";
                    $OurMessage .= "The following outlines your Question and Answer<br /><br />";
                    $OurMessage .= "<b>Question:</b><br />";
                    $OurMessage .= "$qn_notice_desc<br /><br />";
                    $OurMessage .= "<b>Answer:</b><br />";
                    $OurMessage .= "$ans_notice_desc<br /><br />";
                    $OurMessage .= "Regards,<br /><b>OrbitFAQ Mailer</b>";

                    $OurTxtMessage = "Please note that your question raised at  $OrbitFAQTitle has been answered!\n\n";
                    $OurTxtMessage .= "Go to the following link to view the answer to your question\n";
                    $OurTxtMessage .= "$FaqAddress/index.php?faq=$qn_notice_f_id&ca=$qn_notice_c_id#qn$qn_notice_id\n\n";
                    $OurTxtMessage .= "The following outlines your Question and Answer\n\n";
                    $OurTxtMessage .= "Question:\n";
                    $OurTxtMessage .= strip_tags($qn_notice_desc);
                    $OurTxtMessage .= "\n\n";
                    $OurTxtMessage .= "Answer:\n";
                    $OurTxtMessage .= strip_tags($ans_notice_desc);
                    $OurTxtMessage .= "\n\n";
                    $OurTxtMessage .= "Regards,\nOrbitFAQ Mailer\n";

                    // Lets check that they are using swift
                    // if version is ok then use swift
                    if(($phpv == '4')OR($phpv == '5'))
                      {
                        $swift =& new Swift(new Swift_Connection_SMTP("$smtp_hostname"));

                        if($email_format == 'text/html')
                          {
                            $RealMessage = $OurMessage;
                          }
                        else
                          {
                            $RealMessage = $OurTxtMessage;
                          }

                        //Create the message
                        $message =& new Swift_Message("$OrbitFAQTitle Question Answered", "$RealMessage", $email_format);

                        //Now check if Swift actually sends it
                        !$swift->send($message, $qn_notice_posted_email, "$default_email");
                      }

                    // if not then we will just use PHP mail functions.
                    else
                      {
                        $headers = "From: $default_email\r\n";
                        mail( $qn_notice_posted_email, "$OrbitFAQTitle Question Answered", $OurTxtMessage, $headers );
                      }

                      // Set our Notice Sent Flag
                      $query_update = "UPDATE `orbitfaq_questions` SET `notice_sent` = '1' WHERE `id` = '$qn';";
                      $result_update = $faqsql_query ($query_update)OR DIE( "$sql_query_error $query_update");

                  }

                  clearSmartyCompiled($SmartyCompileDir, $ClearSmartyCompiled);

                  $message = "Question Was Successfully Modified/Answered!";
                  header("Location: index.php?mod=$mod&message=$message");

                  exit;

                }
              else
                {

                  $posted_desc = preg_replace( "/\n/", "", $posted_desc);
                  $posted_desc = preg_replace( "/\r/", "", $posted_desc);

                  $posted_ans = preg_replace( "/\n/", "", $posted_ans);
                  $posted_ans = preg_replace( "/\r/", "", $posted_ans);

                  $smarty->assign("posted_associated_answer","$posted_associated_answer");
                  $smarty->assign("posted_id","$qn");
                  $smarty->assign("posted_ans","$posted_ans");
                  $smarty->assign("posted_desc","$posted_desc");
                  $smarty->assign("posted_cat","$posted_cat");
                  $smarty->assign("Errors","$error");

                }

            }
          else
            {

              $query_qn = "SELECT * from orbitfaq_questions WHERE `id` = '$qn' ORDER BY qn_order ASC";
              $result_qn = $faqsql_query ($query_qn)OR DIE( "$sql_query_error $query_qn");

              while ($row_qn = $faqsql_fetch_array ($result_qn)){
                $qn_id = $row_qn[0];
                $qn_f_id = $row_qn[1];
                $posted_cat = $row_qn[2];
                $qn_a_id = $row_qn[3];
                $qn_desc = $row_qn[4];

                $qn_desc = html_entity_decode($qn_desc);
                $qn_desc = preg_replace( "/\n/", "", $qn_desc);
                $qn_desc = preg_replace( "/\r/", "", $qn_desc);

                $query_ans = "SELECT * from orbitfaq_answers WHERE `id` = '$qn_a_id'";
                $result_ans = $faqsql_query ($query_ans)OR DIE( "$sql_query_error $query_ans");
                $count_ans = $faqsql_count_rows($result_ans);

                if($count_ans == '0')
                  {
                    $ans_desc = '';
                  }

                while ($row_ans = $faqsql_fetch_array ($result_ans)){
                  $ans_id = $row_ans[0];
                  $ans_desc = $row_ans[1];
                  $ans_posted_by = $row_ans[2];

                  $ans_desc = html_entity_decode($ans_desc);
                  $ans_desc = preg_replace( "/\n/", "", $ans_desc);
                  $ans_desc = preg_replace( "/\r/", "", $ans_desc);

                }

                // Assign our smarty details
                $smarty->assign("posted_id","$qn_id");
                $smarty->assign("posted_desc","$qn_desc");
                $smarty->assign("posted_ans","$ans_desc");
                $smarty->assign("posted_cat","$posted_cat");

              }

            }

            // Lets get our Categories
            $query_cat_pd = "SELECT * from orbitfaq_categories WHERE `f_id` = ANY(SELECT f_id from orbitfaq_admins_ownership WHERE `admin_id` = '$orbitfaq_userid') ORDER BY f_id, cat_order ASC";
            $result_cat_pd = $faqsql_query ($query_cat_pd)OR DIE( "$sql_query_error $query_cat_pd");
            $count_cat_pd = $faqsql_count_rows($result_cat_pd);

            while ($row_cat_pd = $faqsql_fetch_array ($result_cat_pd)){
              $cat_pd_id = $row_cat_pd[0];
              $cat_pd_f_id = $row_cat_pd[1];
              $cat_pd_title = $row_cat_pd[2];

              $query_faq_pd = "SELECT title from `orbitfaq` WHERE `id` = '$cat_pd_f_id'";
              $result_faq_pd = $faqsql_query ($query_faq_pd)OR DIE( "$sql_query_error $query_faq_pd");
              $count_faq_pd = $faqsql_count_rows($result_faq_pd);

              while ($row_faq_pd = $faqsql_fetch_array ($result_faq_pd)){
                $faq_pd_title = $row_faq_pd[0];
              }

              if((!$posted_cat)AND($qn == $cat_pd_id))
                {
                  $selected = " selected";
                }
              elseif(($posted_cat)AND($cat_pd_id == $posted_cat))
                {
                  $selected = " selected";
                }
              else
                {
                  $selected = "";
                }

              $build_cat_pulldown .= "<option value='$cat_pd_id' $selected>$faq_pd_title > $cat_pd_title</option>\n";

            }

            $smarty->assign("CATPulldown","$build_cat_pulldown");

          }
        else
          {
            clearSmartyCompiled($SmartyCompileDir, $ClearSmartyCompiled);

            $message = "You Did Not Enter a Valid Question Id!";
            header("Location: index.php?mod=$mod&message=$message");
          }
      } // End Modify

    elseif($action == 'delete')
      {
        if($qn != '')
          {

            // Lets check that this user has access to this faq
            $SuperAdmin = CheckAdminAccess($faq, $ca, $qn, $accesslvl, $orbitfaq_userid);

            if($SuperAdmin == '0')
              {

                clearSmartyCompiled($SmartyCompileDir, $ClearSmartyCompiled);

                $message = "You do not have access to delete this Question because it is assigned to a FAQ you do not have access to!";
                header("Location: index.php?mod=$mod&message=$message");

                exit;
              }

          if($posted == 'yes')
            {

              $posted_confirmation = sanitize_paranoid_string($_POST['posted_confirmation']);

              $smarty->assign("posted_cat","$cat_id");

              if(($posted_confirmation == '0')AND(!$posted_cat)){ $error .= " &raquo; You did not select an appropriate <u>Confirmation</u> and <u>New Category</u><br />"; }

              if(!$error)
                {

                  // Delete Answers

                  // Check if the Answer is assigned to multiple Questions
                  $query_ans = "SELECT COUNT(*) FROM `orbitfaq_questions` WHERE a_id = (SELECT id from orbitfaq_answers WHERE `id` = ANY(SELECT a_id from `orbitfaq_questions` WHERE `id` = '$qn'));";
                  $result_ans = $faqsql_query ($query_ans)OR DIE( "$sql_query_error $query_ans");

                  while ($row_ans = $faqsql_fetch_array ($result_ans)){
                    $answ_count = $row_ans[0];

                    if($answ_count <= '1')
                      {
                        $query_delete = "DELETE FROM orbitfaq_answers WHERE `id` = (SELECT a_id from `orbitfaq_questions` WHERE `id` = '$qn');";
                        $result_delete = $faqsql_query ($query_delete)OR DIE( "$sql_query_error $query_delete");
                      }

                  }



                  // Delete Questions
                  $query_delete = "DELETE FROM `orbitfaq_questions` WHERE `id` = '$qn';";
                  $result_delete = $faqsql_query ($query_delete)OR DIE( "$sql_query_error $query_delete");

                  clearSmartyCompiled($SmartyCompileDir, $ClearSmartyCompiled);

                  $message = "Question and Answer was Successfully Deleted! $NEW";
                  header("Location: index.php?mod=$mod&message=$message");

                  exit;

                }

            }

              $query_cat = "SELECT * from orbitfaq_questions WHERE `id` = '$qn'";
              $result_cat = $faqsql_query ($query_cat)OR DIE( "$sql_query_error $query_cat");

              while ($row_cat = $faqsql_fetch_array ($result_cat)){
                $qn_id = $row_cat[0];
                $qn_f_id = $row_cat[1];
                $qn_c_id = $row_cat[2];
                $qn_title = $row_cat[4];

                $qn_title = html_entity_decode($qn_title);
                $qn_title = strip_tags($qn_title);

                // Assign our smarty details
                $smarty->assign("Errors","$error");
                $smarty->assign("posted_id","$qn");
                $smarty->assign("posted_title","$qn_title");

              }

          }
        else
          {
            clearSmartyCompiled($SmartyCompileDir, $ClearSmartyCompiled);

            $message = "You Did Not Enter a Valid Question Id!";
            header("Location: index.php?mod=$mod&message=$message");
          }
      } // End Delete

    elseif($action == 'move')
      {
        if($qn != '')
          {

            if($posted != '')
              { ////order + $move
                $query_update_order = "UPDATE `orbitfaq_questions` SET qn_order = qn_order + $posted WHERE `id` = '$qn';";
                $result_update_order = $faqsql_query ($query_update_order)OR DIE( "$sql_query_error $query_update_order");
              }

            $order = ReOrderQNS($ca);

            clearSmartyCompiled($SmartyCompileDir, $ClearSmartyCompiled);

            $message = "You Have Successfully Moved the Category!";
            header("Location: index.php?mod=$mod&message=$message");
            exit;

          }
        else
          {
            clearSmartyCompiled($SmartyCompileDir, $ClearSmartyCompiled);

            $message = "You Did Not Enter a Valid Question Id!";
            header("Location: index.php?mod=$mod&message=$message");
            exit;
          }
      } // End Move

    elseif($action == 'chstatus')
      {
        if($qn != '')
          {

            // Lets check that this user has access to this faq
            $SuperAdmin = CheckAdminAccess($faq, $ca, $qn, $accesslvl, $orbitfaq_userid);

            if($SuperAdmin == '0')
              {

                clearSmartyCompiled($SmartyCompileDir, $ClearSmartyCompiled);

                $message = "You do not have access to change the status of this Question because it is assigned to a FAQ you do not have access to!";
                header("Location: index.php?mod=$mod&message=$message");

                exit;
              }

            if($posted != '')
              {
                $query_update_order = "UPDATE `orbitfaq_questions` SET status = $posted WHERE `id` = '$qn';";
                $result_update_order = $faqsql_query ($query_update_order)OR DIE( "$sql_query_error $query_update_order");
              }

            clearSmartyCompiled($SmartyCompileDir, $ClearSmartyCompiled);

            $message = "You Have Successfully Change the Question Status!";
            header("Location: index.php?mod=$mod&message=$message");
            exit;

          }
        else
          {

            clearSmartyCompiled($SmartyCompileDir, $ClearSmartyCompiled);

            $message = "You Did Not Enter a Valid Question Id!";
            header("Location: index.php?mod=$mod&message=$message");
            exit;
          }
      } // End Status

    elseif($action == 'ban')
      {
        if(($posted != '')AND(($type != 'email')OR($type != 'ip')))
          {

            $query_add = "INSERT INTO `orbitfaq_banlist` (
                `ban_type`,
                `detail`
              )VALUES(
                '$type',
                '$posted'
              );

            ";

            $result_add = $faqsql_query ($query_add)OR DIE( "$sql_query_error $query_add");

            clearSmartyCompiled($SmartyCompileDir, $ClearSmartyCompiled);

            $message = "You Have Successfully added $posted to the Banlist!";
            header("Location: index.php?mod=$mod&message=$message");
            exit;

          }
        else
          {

            clearSmartyCompiled($SmartyCompileDir, $ClearSmartyCompiled);

            $message = "You Did Not Enter Valid Ban Details!";
            header("Location: index.php?mod=$mod&message=$message");
            exit;
          }
      } // End Status
    elseif($action == 'assocanswer')
      {

        // List all FAQ/Categories/Answers

        $query_faq = "SELECT * from orbitfaq WHERE `status` != '0' ORDER BY faq_order ASC";
        $result_faq = $faqsql_query ($query_faq)OR DIE( "$sql_query_error $query_faq");

        while ($row_faq = $faqsql_fetch_array ($result_faq)){
          $faq_id = $row_faq[0];
          $faq_title = $row_faq[1];
          $faq_desc = $row_faq[2];
          $faq_allow_user_posts = $row_faq[3];
          $faq_public = $row_faq[4];

            // List Categories

            $query_cat = "SELECT * from orbitfaq_categories WHERE `f_id` = '$faq_id' AND `status` != '0' ORDER BY cat_order ASC";
            $result_cat = $faqsql_query ($query_cat)OR DIE( "$sql_query_error $query_cat");
            $count_cat = $faqsql_count_rows($result_cat);

            while ($row_cat = $faqsql_fetch_array ($result_cat)){
              $cat_id = $row_cat[0];
              $cat_f_id = $row_cat[1];
              $cat_title = $row_cat[2];
              $cat_desc = $row_cat[3];

                $query_qn = "SELECT * from orbitfaq_questions WHERE `c_id` = '$cat_id' AND `status` = '1' GROUP BY a_id ORDER BY qn_order ASC";
                $result_qn = $faqsql_query ($query_qn)OR DIE( "$sql_query_error $query_qn");
                $count_qn = $faqsql_count_rows($result_qn);

                while ($row_qn = $faqsql_fetch_array ($result_qn)){
                  $qn_id = $row_qn[0];
                  $qn_f_id = $row_qn[1];
                  $qn_c_id = $row_qn[2];
                  $qn_a_id = $row_qn[3];
                  $qn_desc = $row_qn[4];

                  $query_ans = "SELECT * from orbitfaq_answers WHERE `id` = '$qn_a_id'";
                  $result_ans = $faqsql_query ($query_ans)OR DIE( "$sql_query_error $query_ans");
                  $count_ans = $faqsql_count_rows($result_ans);

                  if($count_ans == '0')
                    {
                      $ans_desc = '';
                    }

                  while ($row_ans = $faqsql_fetch_array ($result_ans)){
                    $ans_id = $row_ans[0];
                    $ans_desc = $row_ans[1];
                    $ans_posted_by = $row_ans[2];

                    $qn_desc = html_entity_decode($qn_desc);
                    $ans_desc = html_entity_decode($ans_desc);

                    if($showAlevel == '1')
                      {
                        $ans_desc = ShortenText($ans_desc, $truncateTo);
                      }

                     // Build Smarty Content Array
                     $smarty_qn_array[] = array(
                            "qn_id" => "$qn_id",
                            "qn_f_id" => "$cat_f_id",
                            "qn_c_id" => "$qn_c_id",
                            "qn_desc" => "$qn_desc",
                            "qn_ans_desc" => "$ans_desc",
                            "qn_ans_id" => "$ans_id"
                      );

                  }

                }

             // Build Smarty Content Array
             $smarty_cat_array[] = array(
                    "cat_id" => "$cat_id",
                    "cat_f_id" => "$cat_f_id",
                    "cat_title" => "$cat_title",
                    "cat_desc" => "$cat_desc",
                    "cat_qn_count" => "$count_qn",
                    "faq_allow_user_posts" => "$faq_allow_user_posts"
              );

            }

           // Build Smarty Content Array
           $smarty_faq_array[] = array(
              "faq_id" => "$faq_id",
              "faq_title" => "$faq_title",
              "faq_cat_count" => "$count_cat"
            );

          }


            // Send our Smarty Data
            $smarty->assign('FAQListing',$smarty_faq_array);
            $smarty->assign('QNListing',$smarty_qn_array);
            $smarty->assign('CATListing',$smarty_cat_array);

      } // End Associate Existing Answer

    }
  else
    {
      $smarty_template_file = "$AdminFullPath/skins/$AdminSkin/mod_". $mod .".tpl";

      if($faq != '')
        {
          $faq_query = " AND `f_id` = '$faq'";

        }

      if($ca != '')
        {
          $ca_query = " AND `c_id` = '$ca'";
        }

      $query_qn = "SELECT * from orbitfaq_questions WHERE `f_id` = ANY(SELECT f_id from orbitfaq_admins_ownership WHERE `admin_id` = '$orbitfaq_userid') $faq_query $ca_query ORDER BY f_id, c_id, qn_order ASC";
      $result_qn = $faqsql_query ($query_qn)OR DIE( "$sql_query_error $query_qn");

      while ($row_qn = $faqsql_fetch_array ($result_qn)){

        $qn_id = $row_qn[0];
        $qn_f_id = $row_qn[1];
        $qn_c_id = $row_qn[2];
        $qn_a_id = $row_qn[3];
        $qn_desc = $row_qn[4];
        $qn_posted_time = $row_qn[5];
        $qn_posted_by = $row_qn[6];
        $qn_posted_email = $row_qn[7];
        $qn_status = $row_qn[8];
        $qn_order = $row_qn[9];
        $qn_ip = $row_qn[10];

        $qn_desc = html_entity_decode($qn_desc);
        $qn_desc = strip_tags($qn_desc);

          $query_ans = "SELECT * from orbitfaq_answers WHERE `id` = '$qn_a_id'";
          $result_ans = $faqsql_query ($query_ans)OR DIE( "$sql_query_error $query_ans");
          $count_ans = $faqsql_count_rows($result_ans);

          if($count_ans == '0')
            {
              $ans_desc = '';
            }

          while ($row_ans = $faqsql_fetch_array ($result_ans)){
            $ans_id = $row_ans[0];
            $ans_desc = $row_ans[1];
            $ans_posted_by = $row_ans[2];

            $ans_desc = html_entity_decode($ans_desc);
            $ans_desc = preg_replace('#<img [^>]*alt="([^"]*)"[^>]*>#i', '[ IMAGE HIDDEN ]', $ans_desc);

          }

          $query_faq = "SELECT title from orbitfaq WHERE `id` = '$qn_f_id'";
          $result_faq = $faqsql_query ($query_faq)OR DIE( "$sql_query_error $query_faq");

          while ($row_faq = $faqsql_fetch_array ($result_faq)){
            $qn_faq_title = $row_faq[0];
          }

          $query_cat = "SELECT title from orbitfaq_categories WHERE `id` = '$qn_c_id'";
          $result_cat = $faqsql_query ($query_cat)OR DIE( "$sql_query_error $query_cat");

          while ($row_cat = $faqsql_fetch_array ($result_cat)){
            $qn_cat_title = $row_cat[0];
          }

        if($qn_row == '1')
          {
            $qn_row = '0';
          }
        else
          {
            $qn_row = '1';
          }



         // Build Smarty Content Array
         $smarty_qn_array[] = array(
            "qn_id" => $qn_id,
            "qn_f_id" => $qn_f_id,
            "qn_f_title" => $qn_faq_title,
            "qn_c_id" => $qn_c_id,
            "qn_c_title" => $qn_cat_title,
            "qn_desc" => $qn_desc,
            "qn_posted_time" => $qn_posted_time,
            "qn_posted_by" => $qn_posted_by,
            "qn_posted_email" => $qn_posted_email,
            "qn_status" => $qn_status,
            "qn_order" => $qn_order,
            "qn_ip" => $qn_ip,
            "qn_ans" => $ans_desc,
            "qn_row" => "$qn_row"
          );

      }

      // Send our Smarty Data
      $smarty->assign('QNListing',$smarty_qn_array);

      //Filter By
      $query_faq = "SELECT * from orbitfaq ORDER BY faq_order ASC";
      $result_faq = $faqsql_query ($query_faq)OR DIE( "$sql_query_error $query_faq");

      while ($row_faq = $faqsql_fetch_array ($result_faq)){
        $faq_id = $row_faq[0];
        $faq_title = $row_faq[1];
        $faq_desc = $row_faq[2];
        $faq_allow_user_posts = $row_faq[3];
        $faq_public = $row_faq[4];
        $faq_order = $row_faq[5];

         // Build Smarty Content Array
         $smarty_faq_filter_array[] = array(
            "faq_pulldown_id" => "$faq_id",
            "faq_pulldown_title" => "$faq_title"
          );

      }

      $smarty->assign('FilterByListing',$smarty_faq_filter_array);

    }
?>